<template>
  <div class="layout">
    <div class="header">
      <div class="header-left">
        <h2>大华数据库文档生成器</h2>
      </div>
      <div class="header-right">
        <el-dropdown>
          <span class="user-info">
            <el-avatar :size="32" :src="userStore.user?.avatar || defaultAvatar">
              {{ userStore.user?.nickname?.charAt(0) || 'U' }}
            </el-avatar>
            <span class="username">{{ userStore.user?.nickname }}</span>
            <el-icon><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="showUserInfo">
                <el-icon><User /></el-icon>
                个人信息
              </el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="main-container">
      <!-- 主内容区 -->
      <main class="main-content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, SwitchButton, ArrowDown } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/userStore'

const router = useRouter()
const userStore = useUserStore()

const defaultAvatar = ''

const showUserInfo = () => {
  ElMessage.info('个人信息功能开发中...')
}

const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })

    userStore.logout()
    ElMessage.success('已退出登录')
    router.push('/login')
  } catch (error) {
    // 用户取消
    console.log('取消退出登录:', error)
  }
}
</script>

<style scoped lang="scss">
.layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px;
  background: #2c3e50;
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: $box-shadow-light;

  .header-left h2 {
    margin: 0;
    color: white;
    font-size: 20px;
    font-weight: 600;
  }

  .user-info {
    display: flex;
    align-items: center;
    cursor: pointer;
    color: white;
    gap: 8px;
    padding: 8px 12px;
    border-radius: $border-radius-base;
    transition: background-color 0.3s;

    &:hover {
      background: rgba(255, 255, 255, 0.1);
    }

    .username {
      font-weight: 500;
    }
  }
}

.main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.sidebar {
  width: 280px;
  background: white;
  border-right: 1px solid $border-light;
  display: flex;
  flex-direction: column;

  .sidebar-content {
    padding: 24px;
    flex: 1;
  }

  .user-card {
    text-align: center;
    padding: 24px 0;
    border-bottom: 1px solid $border-light;
    margin-bottom: 24px;

    .user-avatar {
      margin-bottom: 16px;
      border: 3px solid $primary-color;
    }

    .user-name {
      margin: 0 0 8px 0;
      color: $text-primary;
      font-size: 18px;
      font-weight: 600;
    }

    .user-email {
      margin: 0 0 8px 0;
      color: $text-regular;
      font-size: 14px;
    }

    .user-signature {
      margin: 0;
      color: $text-secondary;
      font-size: 12px;
      font-style: italic;
    }
  }
}

.content {
  flex: 1;
  padding: 48px;
  background: $background-base;
  overflow-y: auto;

  .welcome {
    max-width: 800px;
    margin: 0 auto;

    h1 {
      color: $text-primary;
      margin-bottom: 16px;
      font-size: 36px;
      font-weight: 700;
      text-align: center;
    }

    .welcome-subtitle {
      color: $text-regular;
      font-size: 18px;
      text-align: center;
      margin-bottom: 48px;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .main {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    height: auto;
  }

  .content {
    padding: 24px;
  }
}
.main-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.sidebar {
  width: 240px;
  background: #fff;
  border-right: 1px solid #e6e6e6;
}

.main-content {
  flex: 1;
  padding: 20px;
  overflow: auto;
}
</style>
